package com.ruoyi.project.module.groupProperty.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.module.group.domain.Group;
import com.ruoyi.project.module.group.mapper.GroupMapper;
import com.ruoyi.project.module.group.service.IGroupService;
import com.ruoyi.project.module.groupProperty.domain.GroupProperty;
import com.ruoyi.project.module.groupProperty.mapper.GroupPropertyMapper;
import com.ruoyi.project.module.groupProperty.service.IGroupPropertyService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * com.ruoyi.project.module.groupProperty.controller.GroupPropertyImportController
 * <br>
 *
 * @author 蒋カイセキ    Japan-Tokyo  18-12-13 下午4:46<br>
 * ブログ http://www.shoukaiseki.top/<br>
 * E-メール jiang28555@Gmail.com<br>
 **/
@Controller
@RequestMapping("/module/groupPropertyExport")
public class GroupPropertyExportController extends BaseController {

    private String prefix = "module/groupProperty";


    @Resource
    IGroupService groupService;

    @Resource
    IGroupPropertyService groupPropertyService;

    @Resource
    private GroupPropertyMapper groupPropertyMapper;

    @Resource
    private GroupMapper groupMapper;


    @GetMapping("/export/yaml")
    @Transactional
    public String configGroupPropertyExportYaml(HttpServletResponse response,@RequestParam("configGroupId") Long configGroupId,ModelMap mmap) throws IOException {

        Group group = groupMapper.selectGroupById(configGroupId);
        String propertyPrefix = group.getPropertyPrefix();
        String[] split = propertyPrefix.split("\\.");
        Map map = childMap(configGroupId, 0L);
        for (int i = split.length-1; i >= 0; i--) {
            String key=split[i];
            if(!StringUtils.isEmpty(key)){
                Map mapTemp =new JSONObject();
                mapTemp.put(key,map);
                map=mapTemp;

            }
        }


        Yaml yaml = new Yaml();
        String s = yaml.dumpAsMap(map);

//        byte[] data=s.getBytes(StandardCharsets.UTF_8);
//        String fielname = DigestUtils.md5Hex(data);
//        response.reset();
//        response.setHeader("Content-Disposition", "attachment; filename="+fielname+".json");
//        response.addHeader("Content-Length", "" + data.length);
//        response.setContentType("application/octet-stream; charset=UTF-8");
//
//
//        IOUtils.write(data, response.getOutputStream());
        mmap.put("content", s);
        return prefix + "/exportText";
    }

    public Map childMap(Long configGroupId, Long parentId){
        GroupProperty groupProperty = new GroupProperty();
        groupProperty.setConfigGroupId(configGroupId);
        groupProperty.setParentId(parentId);

        JSONObject jsonObject=new JSONObject();
        List<GroupProperty> groupProperties = groupPropertyMapper.selectGroupPropertyList(groupProperty);
        for (GroupProperty property : groupProperties) {
            jsonObject.put(property.getPropKey(),property.getPropValue());
            List<GroupProperty> groupProperties1 = groupPropertyMapper.selectGroupPropertyListByParentId(property.getConfigGroupPropertyId());
            if(groupProperties1!=null&&groupProperties1.size()>0){
                Map map = childMap(configGroupId, property.getConfigGroupPropertyId());
                jsonObject.put(property.getPropKey(),map);
            }
        }
        return jsonObject;

    }

}
